package com.yunji.news.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunji.news.common.Result;
import com.yunji.news.entity.EnrollmentApplication;
import com.yunji.news.service.EnrollmentApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 报名申请控制器
 *
 * @author yunji
 * @since 2024-11-26
 */
@RestController
@RequestMapping("/enrollment/applications")
@CrossOrigin
public class EnrollmentApplicationController {

    @Autowired
    private EnrollmentApplicationService applicationService;

    /**
     * 分页查询报名申请
     */
    @GetMapping
    public Result<IPage<EnrollmentApplication>> getApplications(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Long majorId,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String keyword) {
        
        IPage<EnrollmentApplication> result = applicationService.getApplicationPage(
            page, size, schoolId, majorId, status, keyword);
        return Result.success(result);
    }

    /**
     * 获取申请详情
     */
    @GetMapping("/{id}")
    public Result<EnrollmentApplication> getApplication(@PathVariable Long id) {
        EnrollmentApplication application = applicationService.getById(id);
        if (application == null) {
            return Result.error("申请不存在");
        }
        return Result.success(application);
    }

    /**
     * 提交报名申请
     */
    @PostMapping
    public Result<String> submitApplication(@RequestBody EnrollmentApplication application) {
        try {
            boolean success = applicationService.submitApplication(application);
            if (success) {
                return Result.success("报名申请提交成功");
            } else {
                return Result.error("报名申请提交失败");
            }
        } catch (Exception e) {
            return Result.error("报名申请提交失败：" + e.getMessage());
        }
    }

    /**
     * 更新申请状态
     */
    @PutMapping("/{id}/status")
    public Result<String> updateStatus(
            @PathVariable Long id,
            @RequestParam String status,
            @RequestParam(required = false) String remark) {
        
        boolean success = applicationService.updateApplicationStatus(id, status, remark);
        if (success) {
            return Result.success("状态更新成功");
        } else {
            return Result.error("状态更新失败");
        }
    }

    /**
     * 更新支付状态
     */
    @PutMapping("/{id}/payment")
    public Result<String> updatePaymentStatus(
            @PathVariable Long id,
            @RequestParam String paymentStatus) {
        
        boolean success = applicationService.updatePaymentStatus(id, paymentStatus);
        if (success) {
            return Result.success("支付状态更新成功");
        } else {
            return Result.error("支付状态更新失败");
        }
    }

    /**
     * 更新考试成绩
     */
    @PutMapping("/{id}/exam-score")
    public Result<String> updateExamScore(
            @PathVariable Long id,
            @RequestParam Integer examScore) {
        
        boolean success = applicationService.updateExamScore(id, examScore);
        if (success) {
            return Result.success("考试成绩更新成功");
        } else {
            return Result.error("考试成绩更新失败");
        }
    }

    /**
     * 批量更新入围状态
     */
    @PutMapping("/batch/final-status")
    public Result<String> batchUpdateFinalStatus(
            @RequestParam Long[] ids,
            @RequestParam String finalStatus) {
        
        boolean success = applicationService.batchUpdateFinalStatus(ids, finalStatus);
        if (success) {
            return Result.success("批量更新成功");
        } else {
            return Result.error("批量更新失败");
        }
    }
}